package base

import com.jfinal.config.*
import com.jfinal.kit.PropKit
import com.jfinal.plugin.activerecord.ActiveRecordPlugin
import com.jfinal.plugin.druid.DruidPlugin
import com.jfinal.template.Engine
import common.autoreview.AutoReview
import controllers.*
import controllers.interceptors.GlobalCORSInterceptor
import models.*

/**
 * JFinal框架配置类
 * */
class DuoyuConfig: JFinalConfig() {

    companion object{
        val prop = PropKit.use("local.properties")
        val DEV_MODE = true
    }

    override fun onStart() {
        AutoReview.init()
    }

    override fun onStop() {
        AutoReview.close()
    }


    override fun configConstant(me: Constants) {
        me.devMode = DEV_MODE
        me.baseDownloadPath = "res"
        me.baseUploadPath = "res"
    }
    /* *
    * Controller路由配置
    * */
    override fun configRoute(me: Routes) {
        me.add("/user", UserController::class.java)
        me.add("/goods", GoodsController::class.java)
        me.add("/res", ResController::class.java)
        me.add("/order", OrderController::class.java)
        me.add("/record", RecordController::class.java)
        me.add("/comment", CommentController::class.java)
        me.add("/admin", AdminController::class.java)
    }

    override fun configPlugin(me: Plugins) {

        val dbUrl = DuoyuConfig.prop.get("db_url")
        val dbUsername = DuoyuConfig.prop.get("db_username")
        val dbPwd = DuoyuConfig.prop.get("db_pwd")
        val druidPlugin = DruidPlugin(dbUrl, dbUsername, dbPwd)
        me.add(druidPlugin)
        val arPlugin = ActiveRecordPlugin(druidPlugin)
        me.add(arPlugin)
        //数据库映射Model
        arPlugin.addMapping(UserM.TABLE_NAME, UserM::class.java)
        arPlugin.addMapping(UserInfoM.TABLE_NAME, UserInfoM::class.java)
        arPlugin.addMapping(ClassificationM.TABLE_NAME, ClassificationM::class.java)
        arPlugin.addMapping(RecordM.TABLE_NAME, RecordM::class.java)
        arPlugin.addMapping(OrderM.TABLE_NAME, OrderM::class.java)
        arPlugin.addMapping(CollectionM.TABLE_NAME, CollectionM::class.java)
        arPlugin.addMapping(BoughtM.TABLE_NAME, BoughtM::class.java)
        arPlugin.addMapping(CommentM.TABLE_NAME, CommentM::class.java)
        arPlugin.addMapping(ReplyM.TABLE_NAME, ReplyM::class.java)
        arPlugin.addMapping(SuperUserM.TABLE_NAME, SuperUserM::class.java)
    }

    override fun configInterceptor(me: Interceptors) {
        me.addGlobalActionInterceptor(GlobalCORSInterceptor())
    }

    override fun configHandler(me: Handlers) {

    }

    override fun configEngine(me: Engine) {

    }
}